package org.spring.springboot.util;


import com.obs.services.ObsClient;
import com.obs.services.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Component
public class ObsUtils {

    private static ObsClient obsClient;
    private static String bucketName;


    @Autowired
    public void setObsClient(ObsClient obsClient) {
        ObsUtils.obsClient = obsClient;
    }

    @Value("${huawei.obs.bucketName}")
    public void setBucketName(String bucketName) {
        ObsUtils.bucketName = bucketName;
    }



    /**
     * 上传文件
     *
     * @param multipartFile 文件
     * @param parentDir 文件目录
     * @param objectKey     文件名，如果桶中有文件夹的话，如往test文件上传test.txt文件，那么objectKey就是test/test.txt
     * @throws Exception
     */
    public static PutObjectResult uploadFile(MultipartFile multipartFile,String parentDir, String objectKey) throws Exception {
        InputStream inputStream = multipartFile.getInputStream();
        PutObjectResult putObjectResult = obsClient.putObject(bucketName, parentDir+"/"+objectKey, inputStream);
        inputStream.close();
        //obsClient.close();
        return putObjectResult;
    }


    public static PutObjectResult uploadFile( InputStream inputStream,String parentDir, String objectKey) throws Exception {
        PutObjectResult putObjectResult = obsClient.putObject(bucketName, parentDir+"/"+objectKey, inputStream);
        inputStream.close();
        //obsClient.close();
        return putObjectResult;
    }

    /**
     * 下载文件
     * @param fileName
     * @return
     */
    public static ObsObject getObject(String fileName){
        ObsObject obsObject = obsClient.getObject(bucketName, fileName);
        return obsObject;
    }
    /**
     * 初始化分段上传任务
     *
     * @param objectName     文件名，如果桶中有文件夹的话，如往test文件上传test.txt文件，那么objectKey就是test/test.txt
     * @throws Exception
     */
    public static String InitiateMultipartUploadRequest(String objectName) throws Exception {
        InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectName);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.addUserMetadata("property", "property-value");
        metadata.setContentType("text/plain");
        request.setMetadata(metadata);
        InitiateMultipartUploadResult result = obsClient.initiateMultipartUpload(request);
        String uploadId = result.getUploadId();
        return uploadId;
    }

    /**
     * 分段上传
     * @param objectName
     * @param uploadId
     * @param partNumber
     * @param multipartFile
     * @throws IOException
     */
    public static UploadPartResult uploadPart(String objectName,String uploadId,Integer partNumber,MultipartFile multipartFile) throws IOException {
        InputStream inputStream = multipartFile.getInputStream();
        return obsClient.uploadPart(bucketName,objectName,uploadId,partNumber,inputStream);
    }


    public static void completeMultipartUpload(String objectName,String uploadId,List<PartEtag> etagList) throws Exception {
        CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(bucketName, objectName, uploadId,etagList);
        obsClient.completeMultipartUpload(request);
    }


    /**
     * 上传文件
     *
     * @param file      文件
     * @param parentDir 文件目录
     * @param objectKey 文件名，如果桶中有文件夹的话，如往test文件上传test.txt文件，那么objectKey就是test/test.txt
     * @throws Exception
     */
    public static PutObjectResult uploadFileByFile(File file,String parentDir, String objectKey) throws Exception {
        InputStream inputStream = new FileInputStream(file);
        PutObjectResult putObjectResult = obsClient.putObject(bucketName, parentDir+"/"+objectKey, inputStream);
        inputStream.close();
        // obsClient.close();
        return putObjectResult;
    }


    /**
     * 文件下载
     * @param parentDir 文件目录
     * @param objectKey 文件名
     * @return
     */
    public static ObsObject downFile(String parentDir,String objectKey){
        ObsObject obsObject = obsClient.getObject(bucketName, parentDir+"/"+objectKey);
        // InputStream ins = obsObject.getObjectContent();

        return obsObject;
    }

    /**
     * 删除文件
     *
     * @param objectKey 文件名，如果桶中有文件夹的话，如往test文件上传test.txt文件，那么objectKey就是test/test.txt
     * @throws Exception
     */
    public static DeleteObjectResult deleteFile(String objectKey) throws Exception {
        DeleteObjectResult deleteObjectResult = obsClient.deleteObject(bucketName, objectKey);
        //obsClient.close();
        return deleteObjectResult;
    }



}
